//会员列表
app.controller('FUserList', function($scope, $http, $location, common){
    $scope.users    = {list: new Array(), page : 0};
    $scope.user     = {};
    $scope.keyword  = '';
    $scope.sort     = '';
    $scope.desc     = 0;
    $scope.pageHtml = '';
    $scope.fatherId = 0;
    $scope.count    = 0;
    $scope.loading  = false;
    $scope.breadcrumb   = new Array({'father_id' : 0, 'nickname' : '全部成员'});
    $scope.dateS    = '';
    $scope.dateE    = '';
    $scope.nUser    = {};

    //初始化JS
    $scope.init_js  = function(){
        $('.input-date').datetimepicker({
            language:  'zh-CN',
            format: 'yyyy-mm-dd',
            weekStart: 1,
            todayBtn:  1,
            autoclose: 1,
            todayHighlight: 1,
            startView: 2,
            forceParse: 0,
            showMeridian: 1,
            minView : 2
        });

        //回车键监听
        common.listenEnterKey(function(){
            $scope.load_list(1);
        });
    }

    //加载用户列表
    $scope.load_list    = function(page, callback){
        var page    = page ? page : 1;
        var url     = config.prefix_url+'/FUser/list?'+
            '&p='+page+
            '&keyword='+$scope.keyword+
            '&sort='+$scope.sort+
            '&desc='+$scope.desc+
            '&date_start='+$scope.dateS+
            '&date_end='+$scope.dateE+
            '&father_id='+$scope.fatherId;

        $http.get(url).success(function(info){
            console.log(info);
            $scope.loading  = false;
            if(info.code != 1){
                common.dealError(info);
            }else{
                $scope.users.page   = page;
                $scope.users.list   = info.data.list;
                $scope.pageHtml     = info.data.page_html;
                if(info.data.count){
                    $scope.count    = info.data.count;
                }
                //启动分页
                common.startPage($scope.load_list);

                if(callback)    callback();
            }
        });
        $scope.loading  = true;
    }

    //用户详情信息
    $scope.detail   = function(userId){
        var url     = config.prefix_url+'/FUser/detail?user_id='+userId;
        $http.get(url).success(function(info){
            $scope.loading  = false;
            if(info.code != 1){
                common.dealError(info);
            }else{
                $scope.user.userInfo    = info.data.user_info;
                $scope.user.relaInfo    = info.data.rela_info;
                $('.modal.user-detail').modal('show');
            }
        });

        $scope.user     = {};
        $scope.loading  = true;
    }
    
    //调整排序顺序
    $scope.change_sort  = function(field){
        $scope.sort     = field;
        var dom     = $('[data-sort="'+field+'"]');
        if($(dom).hasClass('fa-sort-desc')){
            $scope.desc     = 0;
        }else{
            $scope.desc     = 1;
        }
        
        $scope.load_list(1);

        //还原排序图标
        $('.sort').children('.fa')
            .removeClass('fa-sort-desc')
            .removeClass('fa-sort-asc')
            .addClass('fa-sort');
        //排序图标添加
        if($scope.desc){
            $(dom).removeClass('fa-sort').addClass('fa-sort-desc');
        }else{
            $(dom).removeClass('fa-sort').addClass('fa-sort-asc');
        }
    }

    //查看下级用户
    $scope.child_users  = function(fatherId, nickname){
        //面包屑导航处理
        var breadcrumb  = new Array();
        for(var k in $scope.breadcrumb){
            if($scope.breadcrumb[k].father_id == fatherId)
                break;
            breadcrumb.push($scope.breadcrumb[k]);
        }
        breadcrumb.push({'father_id' : fatherId, 'nickname' : nickname});

        $scope.fatherId     = fatherId;
        $scope.keyword  = '';
        $scope.load_list(1, function(){
            $scope.breadcrumb   = breadcrumb;
        });
    }

    //编辑用户信息
    $scope.edit     = function(userId){
        var url     = config.prefix_url+'/FUser/detail?user_id='+userId;
        $http.get(url).success(function(info){
            $scope.loading  = false;
            if(info.code != 1){
                common.dealError(info);
            }else{
                // info.data.rela_info.father_id   = parseInt(info.data.rela_info.father_id);
                for(var k in info.data.rela_info){
                    info.data.rela_info[k]  = parseInt(info.data.rela_info[k]);
                }
                $scope.user     = info.data;
                $('.modal.user-edit').modal('show');
            }
        });

        $scope.user     = {};
        $scope.loading  = true;
    }

    //保存用户信息
    $scope.save     = function(){
        var user    = $scope.user;
        var param   = {
            'user_id'   : user.user_info.id,
            'role_id'   : user.user_info.role_id,
            'father_id' : user.rela_info.father_id,
            'mobile'    : user.user_info.mobile,
            'truename'  : user.user_info.truename,
            'child_school'   : user.user_info.child_school,
            'child_course_name'    : user.user_info.child_course_name
        };
        var url     = config.prefix_url+'/FUser/save';

        $http.post(url, param).success(function(info){
            $scope.loading  = false;
            if(info.code != 1){
                common.dealError(info);
            }else{
                console.log(info);
            }
            $scope.load_list($scope.users.page);
            $('.modal.user-edit').modal('hide');
        });
        $scope.loading  = true;
    }

    //导出excel
    $scope.out_excel    = function(){
        //获取总条目数以便分开获取
        var url     = config.prefix_url+'/FUser/outExcelCount?'+
            '&keyword='+$scope.keyword+
            '&father_id='+$scope.fatherId+
            '&date_start='+$scope.dateS+
            '&date_end='+$scope.dateE;   

        $http.get(url).success(function(info){
            if(info.code != 1){
                common.dealError(info);
            }else{
                var count   = info.data.count;
                var length  = 5000;
                for(var start = 0; start < count; start = start+length){
                    var url     = config.prefix_url+'/FUser/outExcel?'+
                        '&keyword='+$scope.keyword+
                        '&father_id='+$scope.fatherId+
                        '&sort='+$scope.sort+
                        '&desc='+$scope.desc+
                        '&start='+start+
                        '&length='+length+
                        '&date_start='+$scope.dateS+
                        '&date_end='+$scope.dateE;   
                    window.open(url, '_blank');
                }
            }
        });
    }

    //获取VIP信息
    $scope.get_vip_info    = function(user){
        $scope.nUser    = user;
        $('.modal.user-vip').modal('show');
        console.log(user);
    }

    //保存VIP信息
    $scope.update_vip_info  = function(){
        var url     = config.prefix_url + '/FUser/updateItem';
        var param   = {
            'user_id'   : $scope.nUser.id,
            'user_info' : {
                'vip_level' : $scope.nUser.vip_level,
                'vip_enddate'   : $scope.nUser.vip_enddate,
                'vip_artificially_count'    : parseInt($scope.nUser.vip_artificially_count) + 1
            }
        }
        console.log($scope.nUser);
        console.log(param);
        $http.post(url, param).success(function(res){
            $scope.loading  = false;
            if(res.code != 1){
                common.dealError(res);
                return false;
            }
            $('.modal.user-vip').modal('hide');
            $scope.load_list($scope.users.page);
        })

        $scope.loading  = true;
    }

    $scope.load_list();
    $scope.init_js();
});

//vip列表
app.controller('FUserVip', function($scope, $http, $location, common){
    //变量声明
    $scope.list     = new Array();
    $scope.loading  = false;
    $scope.nPage    = 1;
    $scope.pageHtml = '';
    $scope.keyword  = '';
    $scope.sort     = '';
    $scope.desc     = '';

    $scope.init_js  = function(){
        $('.input-date').datetimepicker({
            language:  'zh-CN',
            format: 'yyyy-mm-dd',
            weekStart: 1,
            todayBtn:  1,
            autoclose: 1,
            todayHighlight: 1,
            startView: 2,
            forceParse: 0,
            showMeridian: 1,
            minView : 2
        });

        //回车键监听
        common.listenEnterKey(function(){
            $scope.get_list(1);
        });
    }

    //加载列表
    $scope.get_list     = function(page){
        var page    = page ? page : 1;
        var url     = config.prefix_url + '/FUser/getVipList?p='+page+
            '&sort='+$scope.sort+
            '&desc='+$scope.desc;

        if($scope.keyword)
            url     += '&keyword='+$scope.keyword;

        $http.get(url).success(function(info){
            $scope.loading  = false;
            if(info.code != 1){
                common.dealError(info);
                return false;
            }

            $scope.list     = info.data.list;
            $scope.nPage    = page;
            $scope.pageHtml     = info.data.page_html;

            common.startPage($scope.get_list);
        })

        $scope.loading  = true;
    }

    //条目详情获取
    $scope.get_item     = function(item){
        $scope.nItem    = item;
        $('.modal.user-edit').modal('show');
    }

    //更改信息
    $scope.update_item  = function(userId, userInfo){
        var url     = config.prefix_url + '/FUser/updateItem';
        var param   = {
            'user_id'   : userId,
            'user_info' : {
                'child_name'    : userInfo.child_name,
                'child_birthday_date'   : userInfo.child_birthday_date,
                'child_city'    : userInfo.child_city,
                'child_school'  : userInfo.child_school,
                'child_course_name' : userInfo.child_course_name,
                'child_sex'     : userInfo.child_sex,
                'child_level'   : userInfo.child_level
            }
        }

        $http.post(url, param).success(function(info){
            $scope.loading  = false;
            if(info.code != 1){
                common.dealError(info);
                return false;
            }

            $('.modal.user-edit').modal('hide');
            $scope.load_list($scope.nPage);
        })

        $scope.loading  = true;
    }

    //排序
    $scope.change_sort  = function(field){
        $scope.sort     = field;
        var dom     = $('[data-sort="'+field+'"]');
        if($(dom).hasClass('fa-sort-desc')){
            $scope.desc     = 0;
        }else{
            $scope.desc     = 1;
        }
        
        $scope.get_list(1);

        //还原排序图标
        $('.sort').children('.fa')
            .removeClass('fa-sort-desc')
            .removeClass('fa-sort-asc')
            .addClass('fa-sort');
        //排序图标添加
        if($scope.desc){
            $(dom).removeClass('fa-sort').addClass('fa-sort-desc');
        }else{
            $(dom).removeClass('fa-sort').addClass('fa-sort-asc');
        }
    }

    //导出VIP信息
    $scope.out_excel    = function(){
        var url     = config.prefix_url + '/FUser/outVipExcel?keyword='+$scope.keyword;

        $http.get(url).success(function(res){
            if(res.code != 1){
                common.dealError(res);
                return false;
            }
            var count   = res.data.count;
            var length  = 5000;
            var start  = 0;
            while(start < count){
                var _url     = url + '&start='+start+'&length='+length
                    +'&sort='+$scope.sort
                    +'&desc='+$scope.desc;
                window.open(_url, '_blank');
                start   += length;
            }
        })
    }

    $scope.get_list(1);
    $scope.init_js();
})

//VIP销售记录
app.controller('VipBargainList', function($scope, $http, $location, common){
    //变量声明
    $scope.list     = new Array();
    $scope.nPage    = 1;
    $scope.pageHtml     = '';
    $scope.condition    = {
        date_start  : '',
        date_end    : ''
    }
    $scope.loading  = false;
    $scope.nItem    = new Object();
    $scope.nMoneyInfo   = new Object();
    $scope.nDeductList  = new Object();
    $scope.total    = new Object();

    //初始化js
    $scope.init_js  = function(){
        $('.input-date').datetimepicker({
            language:  'zh-CN',
            format: 'yyyy-mm-dd',
            weekStart: 1,
            todayBtn:  1,
            autoclose: 1,
            todayHighlight: 1,
            startView: 2,
            forceParse: 0,
            showMeridian: 1,
            minView : 2
        });

        //回车键监听
        common.listenEnterKey(function(){
            $scope.get_list();
        });
    }

    //加载数据
    $scope.get_list     = function(page){
        var page    = page ? page : 1;
        var url     = config.prefix_url + '/FUser/getVipBargainList?p='+page;
        var param   = {
            condition   : $scope.condition
        }
        $http.post(url, param).success(function(info){
            $scope.loading  = false;
            console.log(info);
            if(info.code != 1){
                common.dealError(info);
                return false;
            }
            $scope.total    = info.data.total;
            $scope.nPage    = page;
            $scope.list     = info.data.list;
            $scope.pageHtml     = info.data.page_html;

            common.startPage($scope.get_list);
        })

        $scope.loading  = true;
    }

    //导出excel
    $scope.out_excel    = function(){
        var url     = config.prefix_url + '/FUser/outVipBargainExcel?'+
            '&date_start='+$scope.condition.date_start+
            '&date_end='+$scope.condition.date_end;

        $http.get(url).success(function(info){
            console.log(info);
            if(info.code != 1){
                common.dealError(info);
                return false;
            }

            var count   = info.data.count;
            var length  = 1000;
            var start   = 0;
            while(start < count){
                var outUrl  = url + '&start='+start+'&length='+length;
                window.open(outUrl, '_blank');
                start   += length;
            }
        })
    }

    //详情获取
    $scope.get_item     = function(item){
        $scope.nItem    = item;
        var url     = config.prefix_url + '/Money/getDetailItem?id='+item.id;

        $http.get(url).success(function(info){
            $scope.loading  = false;
            if(info.code != 1){
                common.dealError(info);
                return false;
            }
            $scope.nMoneyInfo   = info.data.money_info;
            $scope.nDeductList  = info.data.deduct_info;
            $('.modal.bargain-detail').modal('show');
        });
        $scope.loading  = true;
    }

    $scope.init_js();
    $scope.get_list();
})